<?php

/***
** 学生model
*/
class Student_model extends CI_Model{

 	function __construct(){
    	parent::__construct();
	}

  /**
  ** 获取学生信息
  */
  function get_student_by_userid($user_id){
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_student where user_id = {$user_id} limit 1";
    return $this->db->query($sql)->row_array();
  }

  function update_student_info($user_id,$data)
  {
    //事务开始
    $this->db->trans_start();
    $student_user = $this->get_student_by_userid($user_id);
    if($student_user){
      //存在，update
      $student = array(
        'name' => $data['name'],
        'birthday' => $data['birthday'],
        'province' => $data['province'],
        'city' => $data['city'],
        'district' => $data['district'],
        'sex' => $data['sex'],
        'wx' => $data['wx'],
        'qq' => $data['qq'],
        'edu' => $data['edu'],
        'on_job' => $data['on_job'],
        'address' => $data['address'],
        'class_time' => json_encode( $data['class_time']),
        'remark' => $data['remark'],
      );
      if(!empty($data['phone'])){
        $student['phone'] = $data['phone'];
      }

      $this->db->update('o2o_student', $student,array('student_id' => $student_user['student_id']));
    }else{
      //不存在，添加
      $student = array(
        'user_id' => $user_id,
        'name' => $data['name'],
        'birthday' => $data['birthday'],
        'sex' => $data['sex'],
        'province' => $data['province'],
        'city' => $data['city'],
        'district' => $data['district'],
        'wx' => $data['wx'],
        'qq' => $data['qq'],
        'edu' => $data['edu'],
        'on_job' => $data['on_job'],
        'workstation' => '',
        'class_time' => json_encode( $data['class_time']),
        'address' => $data['address'],
        'remark' => $data['remark'],
        'add_time' => time()
      );
      if(!empty($data['phone'])){
        $student['phone'] = $data['phone'];
      }
      $this->db->insert('o2o_student', $student);
    }
    //更新user表nickname
    $user = array('nick' => $data['name'] );
    if(!empty($data['head_image_url'])){
      $user['head_image_url'] = $data['head_image_url'];
    }
    $this->db->update('user', $user,array('id' => $user_id));

    $this->db->trans_complete();
    return 1;
  }

  /**
   * 添加学员信息
   * @param [type] $user_id [description]
   * @param [type] $data    [description]
   */
  public function add_student_info($user_id,$data)
  {
    //事务开始
    $this->db->trans_start();

    //判断学生信息是否存在
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_student where user_id = {$user_id} limit 1";
    $student_info = $this->db->query($sql)->row_array();
    if(empty($student_info)){
      //不存在，添加
      $student = array(
        'user_id' => $user_id,
        'name' => $data['name'],
        'phone' => $data['phone'],
        'birthday' => $data['birthday'],
        'sex' => $data['sex'],
        'wx' => $data['wx'],
        'qq' => $data['qq'],
        'edu' => $data['edu'],
        'on_job' => $data['on_job'],
        'workstation' => '',
        'class_time' => json_encode( $data['class_time']),
        'address' => $data['address'],
        'remark' => $data['remark'],
        'add_time' => time()
      );

      $this->db->insert('o2o_student', $student);
    }else{
      //存在，update
      $student = array(
        'name' => $data['name'],
        'phone' => $data['phone'],
        'birthday' => $data['birthday'],
        'sex' => $data['sex'],
        'wx' => $data['wx'],
        'qq' => $data['qq'],
        'edu' => $data['edu'],
        'on_job' => $data['on_job'],
        'address' => $data['address'],
        'class_time' => json_encode( $data['class_time']),
        'remark' => $data['remark'],
      );

      $this->db->update('o2o_student', $student,array('student_id' => $student_info['student_id']));
    }

    //更新user表nickname
    $user = array('nick' => $data['name'] );
    $this->db->update('user', $user,array('id' => $user_id));

    $this->db->trans_complete();
    return 1;
  }
}
